package net.jescort.persistence.dao.eclipselink;

import java.util.List;
import javax.persistence.Query;
import net.gelif.kernel.persistence.dao.eclipselink.GenericEclipseLinkDao;
import net.jescort.domain.forum.Message;
import net.jescort.persistence.dao.MessageDao;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;


@Repository("messageDao")
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class MessageJpaDao extends GenericEclipseLinkDao<Message, Integer> implements MessageDao
{
    @Override
    public long countBySenderId(int senderId)
    {
        return (Long)entityManager.createNativeQuery("select count(id) from messages where sender_id = ?").setParameter(1, senderId).getSingleResult();
    }
    
    @SuppressWarnings("unchecked")
    public List<Message> findBySenderId(int senderId)
    {
        return entityManager.createNativeQuery("select * from messages where sender_id = ?").setParameter(1, senderId).getResultList();
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Message> findBySenderId(int senderId, Pageable pageable)
    {
        String jpql = "select * from messages where sender_id = ?";
        Query query = entityManager.createNativeQuery(jpql);
        
        query.setParameter(1, senderId);
        query.setFirstResult(pageable.getOffset());
        query.setMaxResults(pageable.getPageSize());
        return query.getResultList();
    }
    
    @SuppressWarnings("unchecked")
    public List<Message> findBySenderId(int senderId, int offset, int limit)
    {
        Query query = entityManager.createNativeQuery("select * from messages where sender_id = ?");
        query.setParameter(1, senderId);
        query.setFirstResult(offset);
        query.setMaxResults(limit);
        return query.getResultList();
    }
    
    @Override
    public long countByRecipientId(int recipientId)
    {
        Query query = entityManager.createNativeQuery("SELECT count(m.id) FROM messages m INNER JOIN message_recipient_map map ON map.message_id = m.id WHERE map.recipient_id = " + recipientId);
        return (Long)query.getSingleResult();
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Message> findByRecipientId(int recipientId)
    {
        Query query = entityManager.createNativeQuery("SELECT m.* FROM messages m INNER JOIN message_recipient_map map ON map.message_id = m.id WHERE map.recipient_id = " + recipientId);
        return query.getResultList();
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Message> findByRecipientId(int recipientId, Pageable pageable)
    {
        Query query = entityManager.createNativeQuery("SELECT m.* FROM messages m INNER JOIN message_recipient_map map ON map.message_id = m.id WHERE map.recipient_id = " + recipientId);
        query.setFirstResult(pageable.getOffset());
        query.setMaxResults(pageable.getPageSize());
        return query.getResultList();
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Message> findByRecipientId(int recipientId, int offset, int limit)
    {
        Query query = entityManager.createNativeQuery("SELECT m.* FROM messages m INNER JOIN message_recipient_map map ON map.message_id = m.id WHERE map.recipient_id = " + recipientId);
        query.setFirstResult(offset);
        query.setMaxResults(limit);
        return query.getResultList();
    }
}
